home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 1146 / 1146.xpi / chrome / screengrab.jar / content / Selection.js < prev    next >
Text File  |  2009-03-09  |  5KB  |  140 lines

  1. /*
  2. Copyright (C) 2004-2007  Andy Mutton
  3.  
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License
  6. as published by the Free Software Foundation; either version 2
  7. of the License, or (at your option) any later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  17.  
  18. Contact: andy@5263.org
  19. */
  20. SGSelection = {
  21.     BACKGROUND_DIV : "screengrabBackgroundDiv",
  22.     DRAW_DIV : "screengrabDrawDiv",
  23.     BOX_DIV : "screengrabBoxDiv",
  24.     IDLE_IMAGE : "url('chrome://screengrab/skin/idle.png') 0 no-repeat",
  25.     SNAP_IMAGE : "url('chrome://screengrab/skin/snap.png') 0 no-repeat",
  26.     TOOL_TEXT : "Grab/Cancel",
  27.     oldText : null,
  28.     drawing : false,
  29.     browser : null,
  30.     
  31.     toggleDraw : function() {
  32.         if (this.drawing) {
  33.             this.disableDrawAndGrabIfRequired();
  34.         } else {
  35.             this.enableDraw();
  36.         }
  37.     },
  38.     
  39.     insertHeaderElements : function() {
  40.         var x = window._content;
  41.         var pageHead = x.document.getElementsByTagName("head")[0];
  42.     
  43.         if (pageHead == null) { // if page head doesn't exist, create one
  44.           var pageBody = x.document.getElementsByTagName("html")[0];
  45.           var pageHead = x.document.createElement("head");
  46.     
  47.           pageBody.appendChild(pageHead);
  48.     
  49.           var pageHead = x.document.getElementsByTagName("head")[0];
  50.         }
  51.     
  52.         var cssCheck = x.document.getElementById("screengrab_css");
  53.         var jsCheck = x.document.getElementById("screengrab_js");
  54.     
  55.         if (cssCheck == null) { // insert stylesheet reference
  56.           var css = x.document.createElement("link");
  57.           css.setAttribute("id", "screengrab_css");
  58.           css.setAttribute("rel", "stylesheet");
  59.           css.setAttribute("type", "text/css");
  60.           css.setAttribute("href", "chrome://screengrab/skin/screengrab.css");
  61.     
  62.           pageHead.appendChild(css);
  63.         }
  64.     
  65.         if (jsCheck == null) { // insert javascript reference
  66.           var js = x.document.createElement("script");
  67.           js.setAttribute("id", "screengrab_js");
  68.           js.setAttribute("language", "JavaScript");
  69.           js.setAttribute("type", "text/javascript");
  70.           js.setAttribute("src", "chrome://screengrab/content/injected/selectionBox.js");
  71.     
  72.           pageHead.appendChild(js);
  73.         }
  74.     },
  75.     
  76.     enableDraw : function() {
  77.         var screengrabBar = window.document.getElementById("screengrab_bar");
  78.         if (screengrabBar != null) {
  79.             screengrabBar.style.background = this.SNAP_IMAGE;
  80.             this.oldText = screengrabBar.tooltiptext;
  81.             screengrabBar.tooltiptext = this.TOOL_TEXT;
  82.         }
  83.     
  84.         this.insertHeaderElements();
  85.         var winCon = window._content;
  86.     
  87.         var body = winCon.document.getElementsByTagName("html")[0];
  88.         
  89.         var drawDiv = winCon.document.createElement("div");
  90.         drawDiv.setAttribute("id", this.DRAW_DIV);
  91.         
  92.         var backgroundDiv = winCon.document.createElement("div");
  93.         backgroundDiv.setAttribute("id", this.BACKGROUND_DIV);
  94.         backgroundDiv.setAttribute("class", "backgroundOverlay");
  95.         backgroundDiv.setAttribute("onmousedown", "beginBoxDraw(event)");
  96.         
  97.         drawDiv.appendChild(backgroundDiv);
  98.         body.appendChild(drawDiv);
  99.         
  100.         window._content.document.addEventListener("mouseup", SGSelectionOnMouseUpHandlerThatHasToExistForSomeReason, true);
  101.         this.drawing = true;
  102.     },
  103.     
  104.     disableDrawAndGrabIfRequired : function(event) {
  105.         try {
  106.             window._content.document.removeEventListener("mouseup", SGSelectionOnMouseUpHandlerThatHasToExistForSomeReason, true);
  107.             var winCon = window._content;
  108.             var dimBox = null;
  109.             // create a box to hold the dimensions of the box
  110.             var box = winCon.document.getElementById(this.BOX_DIV);
  111.             if (box != null) {
  112.                 dimBox = new screengrab.Box(box.offsetLeft, box.offsetTop, box.clientWidth, box.clientHeight)
  113.             }
  114.             // remove the box div
  115.             var body = winCon.document.getElementsByTagName("html")[0];
  116.             var newDiv = winCon.document.getElementById(this.DRAW_DIV);
  117.             body.removeChild(newDiv);
  118.             
  119.             // restore the styling to the screengrab bar
  120.             var screengrabBar = window.document.getElementById("screengrab_bar");
  121.             if (screengrabBar != null) {
  122.                 screengrabBar.style.background = this.IDLE_IMAGE;
  123.                 screengrabBar.tooltiptext = this.oldText;
  124.             }
  125.             
  126.             this.drawing = false;
  127.             
  128.             // take the shot (hopefully everything is clean now)
  129.             if (box != null && event != null) {
  130.                 SGSelection.callback(dimBox);
  131.             }
  132.         } catch (error) {
  133.             screengrab.error(error);
  134.         }
  135.     }
  136. }
  137.  
  138. function SGSelectionOnMouseUpHandlerThatHasToExistForSomeReason(event) {
  139.     SGSelection.disableDrawAndGrabIfRequired(event);
  140. }